home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 353_01 / chap09.txt < prev    next >
Text File  |  1992-01-18  |  23KB  |  531 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.                                                         Chapter 9
  8.                        MULTIPLE INHERITANCE AND FUTURE DIRECTIONS
  9.  
  10. C++ version 2.0 was released by AT&T during the summer of 1989, and
  11. the major addition to the language is multiple inheritance, the
  12. ability to inherit data and methods from more than one class into
  13. a subclass.  Multiple inheritance and a few of the other additions
  14. to the language will be discussed in this chapter along with some
  15. of the expected future directions of the language.
  16.  
  17. Several companies have C++ compilers available in the marketplace,
  18. and many others are sure to follow.  Because the example programs
  19. in this tutorial are designed to be as generic as possible, most
  20. should be compilable with any good quality C++ compiler provided
  21. it follows the AT&T definition of version 2.1 or newer.  Many of
  22. these examples will not work with earlier definitions because the
  23. language was significantly changed with the version 2.1 update.
  24.  
  25. After completing this tutorial, you should have enough experience
  26. with the language to study additional new constructs on your own
  27. as they are implemented by the various compiler writers.  We will
  28. update the entire tutorial as soon as practical following
  29. procurement of any new compiler, but hopefully the language will
  30. not change rapidly enough now to warrant an update oftener than
  31. annually.  Please feel free to contact us for information on
  32. updates to the Coronado Enterprises C++ tutorial.
  33.  
  34.  
  35. MULTIPLE INHERITANCE
  36. _________________________________________________________________
  37.  
  38. A major recent addition to the C++ language is the ability to
  39. inherit methods and variables from two or more parent classes when
  40. building a new class.  This is called multiple inheritance, and is
  41. purported by many people to be a major requirement for an object
  42. oriented programming language.  Some writers, however, have
  43. expressed doubts as to the utility of multiple inheritance.  To
  44. illustrate the validity of this, it was not easy to think up a good
  45. example of the use of multiple inheritance as an illustration for
  46. this chapter.  In fact, the resulting example is sort of a forced
  47. example that really does nothing useful.  It does however,
  48. illustrate the mechanics of the use of multiple inheritance with
  49. C++, and that is our primary concern at this time.
  50.  
  51. The biggest problem with multiple inheritance involves the
  52. inheritance of variables or methods from two or more parent classes
  53. with the same name.  Which variable or method should be chosen as
  54. the inherited variable or method if two or more have the same name?
  55. This will be illustrated in the next few example programs.
  56.  
  57.  
  58.                                                          Page 9-1
  59.  
  60.            Chapter 9 - Multiple Inheritance and Future Directions
  61.  
  62.  
  63. SIMPLE MULTIPLE INHERITANCE
  64. _________________________________________________________________
  65.  
  66. An examination of the file named MULTINH1.CPP    ================
  67. will reveal the definition of two very simple      MULTINH1.CPP
  68. classes in lines 4 through 27 named moving_van   ================
  69. and driver.
  70.  
  71. In order to keep the program as simple as possible, all of the
  72. member methods are defined as inline functions.  This puts the code
  73. for the methods where it is easy to find and study.  You will also
  74. notice that all variables in both classes are declared to be
  75. protected so they will be readily available for use in any class
  76. which inherits them.  The code for each class is kept very simple
  77. so that we can concentrate on studying the interface to the methods
  78. rather than spending time trying to understand complex methods.
  79. As mentioned previously, chapter 12 will illustrate the use of non-
  80. trivial methods.
  81.  
  82. In line 30, we define another class named driven_truck which
  83. inherits all of the data and all of the methods from both of the
  84. previously defined classes.  In the last two chapters, we studied
  85. how to inherit a single class into another class, and to inherit
  86. two or more classes, the same technique is used except that we use
  87. a list of inherited classes separated by commas as illustrated in
  88. line 30.  The observant student will notice that we use the keyword
  89. public prior to the name of each inherited class in order to be
  90. able to freely use the methods within the subclass.  In this case,
  91. we didn't define any new variables, but we did introduce two new
  92. methods into the subclass in lines 32 through 39.
  93.  
  94. We declared an object named chuck_ford which presumably refers to
  95. someone named Chuck who is driving a Ford moving van.  The object
  96. named chuck_ford is composed of four variables, three from the
  97. moving_van class, and one from the driver class.  Any of these four
  98. variables can be manipulated in any of the methods defined within
  99. the driven_truck class in the same way as in a singly inherited
  100. situation.  A few examples are given in lines 47 through 56 of the
  101. main program and the diligent student should be able to add
  102. additional output messages to this program if he understands the
  103. principles involved.
  104.  
  105. All of the rules for private or protected variables and public or
  106. private method inheritance as used with single inheritance extends
  107. to multiple inheritance.
  108.  
  109.  
  110. DUPLICATED METHOD NAMES
  111. _________________________________________________________________
  112.  
  113. You will notice that both of the parent classes have a method named
  114. initialize(), and both of these are inherited into the subclass
  115. with no difficulty.  However, if we attempt to send a message to
  116.  
  117.                                                          Page 9-2
  118.  
  119.            Chapter 9 - Multiple Inheritance and Future Directions
  120.  
  121. one of these methods, we will have a problem, because the system
  122. does not know which we are referring to.  This problem will be
  123. solved and illustrated in the next example program.
  124.  
  125. Before going on to the next example program, it should be noted
  126. that we have not declared any objects of the two parent classes in
  127. the main program.  Since the two parent classes are simply normal
  128. classes themselves, it should be apparent that there is nothing
  129. magic about them and they can be used to define and manipulate
  130. objects in the usual fashion.  You may wish to do this to review
  131. your knowledge of simple classes and objects of those classes.
  132.  
  133. Be sure to compile and execute this program after you understand
  134. its operation completely.
  135.  
  136.  
  137.  
  138. MORE DUPLICATE METHOD NAMES
  139. _________________________________________________________________
  140.  
  141. The second example program in this chapter named ================
  142. MULTINH2.CPP, illustrates the use of classes       MULTINH2.CPP
  143. with duplicate method names being inherited into ================
  144. a derived class.
  145.  
  146. If you study the code, you will find that a new method has been
  147. added to all three of the classes named cost_per_full_day().  This
  148. was done intentionally to illustrate how the same method name can
  149. be used in all three classes.  The class definitions are no problem
  150. at all, the methods are simply named and defined as shown.  The
  151. problem comes when we wish to use one of the methods since they are
  152. all the same name and they have the same numbers and types of
  153. parameters and identical return types.  This prevents some sort of
  154. an overloading rule to disambiguate the message sent to one or more
  155. of the methods.
  156.  
  157. The method used to disambiguate the method calls are illustrated
  158. in lines 60, 64, and 68 of the main program.  The solution is to
  159. prepend the class name to the method name with the double colon as
  160. used in the method implementation definition.  This is referred to
  161. as qualifying the method name.  Qualification is not necessary in
  162. line 68 since it is the method in the derived class and it will
  163. take precedence over the other method names.  Actually, you could
  164. qualify all method calls, but if the names are unique, the compiler
  165. can do it for you and make your code easier to write and read.
  166.  
  167. Be sure to compile and execute this program and study the results.
  168. The observant student will notice that there is a slight
  169. discrepancy in the results given in lines 79 through 81, since the